import {
  Toast
} from "vant";

export default {
  addCounter(state, payload) {
    payload.count++;
    Toast({
      type: "success",
      message: `当前商品数量为${payload.count}`,
      // 弹框的时候禁止点击
      forbidClick: true,
      duration: 1500
    });
    localStorage.setItem("cartList", JSON.stringify(state.cartList))
  },
  // 往购物车里放数据
  addToCart(state, payload) {
    payload.count = 1
    payload.checked = false
    state.cartList.push(payload)
    Toast({
      type: "success",
      message: `加入购物车成功`,
      // 弹框的时候禁止点击
      forbidClick: true,
      duration: 1000
    })
    localStorage.setItem("cartList", JSON.stringify(state.cartList));
  },
  // 点击提交，删除对应数据
  clearCart(state) {
    // 判断选中哪些数据,过滤没选中的数组返回一个新数组即可
    let result = state.cartList.filter(item => item.checked !== true);
    if (result.length === 0) {
      localStorage.removeItem("cartList");
      state.cartList = [];
    } else {
      state.cartList = result;
      localStorage.setItem("cartList", JSON.stringify(state.cartList));
    }
  },
  // 设置请求加载
  setLoading(state, bol) {
    state.isLoading = bol;
  },
  //给购物车设置本地存储
  setCartList(state, payload) {
    state.cartList = payload
  }
}